/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package View;

import controller.VinculoController;
import exception.DuplicateConstraintException;
import exception.RemoveConstraintException;
import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.inject.Inject;
import javax.inject.Named;
import model.entidades.Vinculo;
import org.apache.myfaces.extensions.cdi.core.api.scope.conversation.Conversation;
import org.apache.myfaces.extensions.cdi.core.api.scope.conversation.ConversationScoped;

/**
 *
 * @author helton
 */
@Named
@ConversationScoped
public class VinculoView implements View, Serializable {

    @Inject
    Conversation conversation;
    @Inject
    VinculoController controller;
    @Inject
    ProfissionalView profissionalView;
    private Vinculo vinculo;
    private List<Vinculo> vinculos;
    private Map<String, String> tipVinculo = new HashMap<>();
    private Map<String, String> esfera = new HashMap<>();
    private Map<String, String> semana = new HashMap<>();

    @Override
    @PostConstruct
    public void init() {
        vinculo = new Vinculo();
    }

    @Override
    public void closeConversation(ActionEvent event) {
        conversation.close();
    }

    @Override
    public String actionNovo() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public String actionSalvar() throws DuplicateConstraintException {
        if (validaForm()) {
            vinculo.setIdProfis(profissionalView.getProfissional().getId());
            if (vinculo.getId() == null) {
                controller.insert(vinculo);
                init();
            } else {
                controller.update(vinculo);
                init();
            }
            vinculos = null;
            return "vinculos";
        } else {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(
                    FacesMessage.SEVERITY_ERROR, "Preencha os campos obrigatórios (*)!", ""));
            return "vinculos";
        }
    }

    @Override
    public String actionListar() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public String seguinte() {
        conversation.close();
        return "desejos";
    }

    public String voltar() {
        return "formacoes";
    }

    @Override
    public void actionRemover() throws RemoveConstraintException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public boolean validaForm() {
        if (vinculo.getTipo() == null || vinculo.getUnidade() == null
                || vinculo.getEsfera() == null || vinculo.getCargaHoraria() == 0) {
            return false;
        } else {
            return true;
        }
    }

    public Vinculo getVinculo() {
        return vinculo;
    }

    public void setVinculo(Vinculo vinculo) {
        this.vinculo = vinculo;
    }

    public List<Vinculo> getVinculos() {
        if (vinculos == null) {
            vinculos = controller.getAll(profissionalView.getProfissional().getId());
        }
        return vinculos;
    }

    public void setVinculos(List<Vinculo> vinculos) {
        this.vinculos = vinculos;
    }

    public Map<String, String> getTipVinculo() {
        tipVinculo.put("Contrato", "Contrato");
        tipVinculo.put("Concurso", "Concurso");
        return tipVinculo;
    }

    public void setTipVinculo(Map<String, String> tipVinculo) {
        this.tipVinculo = tipVinculo;
    }

    public Map<String, String> getEsfera() {
        esfera.put("Municipal", "Municipal");
        esfera.put("Estadual", "Estadual");
        esfera.put("Federal", "Federal");
        return esfera;
    }

    public void setEsfera(Map<String, String> esfera) {
        this.esfera = esfera;
    }
}
